part of YFree;

BuildContext? _dialogContext;

// dismissible为false时，要调用yDismissDialog_unDismissible关闭对话框
void yShowDialog({required Function(BuildContext pageContext, BuildContext? dialogContext) alertBuilder, bool dismissible = true, BuildContext? context}) {
  showDialog(
    context: context ?? yPages.last.context,
    barrierDismissible: dismissible,
    builder: (dialogContext) {
      if (!dismissible) _dialogContext = dialogContext;
      return WillPopScope(
        onWillPop: () => Future.value(dismissible),
        child: alertBuilder(context ?? yPages.last.context, dismissible ? dialogContext : null),
      );
    },
  );
}

// dismissible为false时，要调用yDismissDialog_unDismissible关闭对话框
void yShowStatefulDialog({required Function(BuildContext pageContext, BuildContext? dialogContext, BuildContext statefulContext, StateSetter setDialogState) alertBuilder, bool dismissible = true, BuildContext? context}) {
  showDialog(
    context: context ?? yPages.last.context,
    barrierDismissible: dismissible,
    builder: (dialogContext) {
      if (!dismissible) _dialogContext = dialogContext;
      return WillPopScope(
        onWillPop: () => Future.value(dismissible),
        child: StatefulBuilder(
          builder: (statefulContext, setDialogState) => alertBuilder(context ?? yPages.last.context, dismissible ? dialogContext : null, statefulContext, setDialogState),
        ),
      );
    },
  );
}

void yDismissDialog_unDismissible() {
  if (_dialogContext != null) {
    var state = Navigator.of(_dialogContext!);
    if (state.canPop()) state.pop();
    _dialogContext = null;
  }
}

void yDismissDialog(BuildContext? dialogContext) {
  if (dialogContext != null) {
    var state = Navigator.of(dialogContext);
    if (state.canPop()) state.pop();
  }
}

///弹出对话框
void yShowBuilder(String message, Function() confirmCallBack, Function()? cancelCallBack, bool cancelAble, {String posTitle = "确定", String negTitle = "取消", BuildContext? context}) async {
  showDialog(
    context: context ?? yPages.last.context,
    barrierDismissible: false,
    builder: (context) {
      return WillPopScope(
        onWillPop: () => Future.value(false),
        child: AlertDialog(
          title: Text("${YConfig.appName}提示", style: const TextStyle(color: Colors.black)),
          content: SingleChildScrollView(child: Text(message, style: const TextStyle(color: Colors.black))),
          actions: [
            YFlatButton(Text(posTitle, style: const TextStyle(color: Colors.black)), onClick: () {
              confirmCallBack();
              Navigator.of(context).pop();
            }),
            Offstage(
              offstage: !cancelAble && cancelCallBack == null,
              child: YFlatButton(
                Text(negTitle, style: const TextStyle(color: Colors.black)),
                onClick: cancelCallBack != null
                    ? () {
                        cancelCallBack();
                        Navigator.of(context).pop();
                      }
                    : () {
                        Navigator.of(context).pop();
                      },
              ),
            ),
          ],
        ),
      );
    },
  );
}

///弹出actionsheet
void yShowActionSheet(String message, List<String> actions, Function(int index, String action) onCallBack, {Function()? onCancel, BuildContext? context}) async {
  final result = await showModalActionSheet<String>(
    context: context ?? yPages.last.context,
    actions: actions.map<SheetAction<String>>((e) => SheetAction(key: e, label: e)).toList(),
    isDismissible: true,
    useRootNavigator: true,
    cancelLabel: "取消",
    message: message,
    title: "请选择",
  );
  var i = 0;
  int? index;
  for (var element in actions) {
    if (element == result) index = i;
    i++;
  }
  if (index != null && result != null) {
    onCallBack(index, result);
  } else {
    onCancel?.call();
  }
}

///弹出单选框
void yShowSelectBuilder(String message, bool cancelAble, List<String> actions, Function(int index, String action) onCallBack, {Function()? onCancelBack, BuildContext? context}) async {
  final result = await showConfirmationDialog(
    context: context ?? yPages.last.context,
    message: message,
    title: "请选择",
    cancelLabel: cancelAble ? "取消" : null,
    actions: actions.map<AlertDialogAction<String>>((e) => AlertDialogAction(key: e, label: e)).toList(),
    useRootNavigator: true,
    barrierDismissible: false,
    okLabel: "确定",
  );
  var i = 0;
  int? index;
  for (var element in actions) {
    if (element == result) index = i;
    i++;
  }
  onCallBack(index!, result!);
}
